﻿Imports Microsoft.Win32

Class HeapView

    Private mVm As HeapViewModel

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        mVm = New HeapViewModel
        Me.DataContext = mVm

    End Sub

    Private Sub AddSnapshotButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim add As New AddSnapshotView()
        add.ShowDialog()
        If Not add.Snapshots Is Nothing Then
            For Each s As HeapSnapshot In add.Snapshots
                mVm.AddSnapshot(s)
            Next
        End If
    End Sub

    Private Sub RemoveSnapshotButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim source As Button = DirectCast(sender, Button)
        Dim snapshot As HeapSnapshot = DirectCast(source.CommandParameter, HeapSnapshot)
        mVm.RemoveSnapshot(snapshot)
    End Sub

    Private Sub MenuOpen_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim dialog As New OpenFileDialog
        With dialog
            .DefaultExt = ".hvw"
            .Filter = "Heap View files (*.hvw) |*.hvw"
        End With
        If dialog.ShowDialog() Then
            Dim savePath As String = dialog.FileName
            mVm.Open(savePath)
        End If

    End Sub

    Private Sub MenuSave_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim dialog As New SaveFileDialog
        With dialog
            .DefaultExt = ".hvw"
            .Filter = "Heap View files (*.hvw) |*.hvw"
        End With
        If dialog.ShowDialog() Then
            Dim savePath As String = dialog.FileName
            mVm.Save(savePath)
        End If
    End Sub

    Private Sub MenuPrint_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim preview As New PrintView(mVm)
        preview.Show()
    End Sub

    Private Sub MenuExportCSV_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim dialog As New SaveFileDialog
        With dialog
            .DefaultExt = ".csv"
            .Filter = "CSV files (*.csv) |*.csv"
        End With
        If dialog.ShowDialog() Then
            Dim savePath As String = dialog.FileName
            mVm.ExportToCSV(savePath)
        End If

    End Sub

    Private Sub MenuExportDOCX_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Throw New NotImplementedException()
    End Sub

    Private Sub MenuFilterType_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim filter As String = InputBox("Enter filter string", "Filter", "heap")
        mVm.FilterTypeName(filter)
    End Sub

    Private Sub DumpInfoButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim win As New DumpView(mVm.Snapshots.ToList)
        win.Show()
    End Sub

    Private Sub MoveUp_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim source As Button = DirectCast(sender, Button)
        Dim snapshot As HeapSnapshot = DirectCast(source.CommandParameter, HeapSnapshot)
        mVm.MoveSnapshotUp(snapshot)

    End Sub

    Private Sub MoveDown_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim source As Button = DirectCast(sender, Button)
        Dim snapshot As HeapSnapshot = DirectCast(source.CommandParameter, HeapSnapshot)
        mVm.MoveSnapshotDown(snapshot)


    End Sub

    Private Sub CloseSet_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        mVm.CloseSet()
    End Sub

    Private Sub OptionsOpen_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim win As New SettingsView()
        win.ShowDialog()
    End Sub

End Class
